package com.ims.core.doc.service.impl;

import java.util.List;
import com.ims.common.utils.DateUtils;
import com.ims.common.utils.uuid.IdUtils;
import com.ims.core.doc.domain.TScSettlebillEntry;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ims.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ims.core.doc.domain.TSalOrderEntry;
import com.ims.core.doc.mapper.TSalOrderMapper;
import com.ims.core.doc.domain.TSalOrder;
import com.ims.core.doc.service.ITSalOrderService;
import org.springframework.util.CollectionUtils;

/**
 * 销售单Service业务层处理
 *
 * @author IMS
 * @date 2024-12-02
 */
@Service
public class TSalOrderServiceImpl implements ITSalOrderService
{
    @Autowired
    private TSalOrderMapper tSalOrderMapper;

    /**
     * 查询销售单
     *
     * @param id 销售单主键
     * @return 销售单
     */
    @Override
    public TSalOrder selectTSalOrderById(Long id)
    {
        return tSalOrderMapper.selectTSalOrderById(id);
    }

    @Override
    public TSalOrder selectTSalOrderByOrderNo(String orderNo) {
        return tSalOrderMapper.selectTSalOrderByOrderNo(orderNo);
    }

    /**
     * 查询销售单列表
     *
     * @param tSalOrder 销售单
     * @return 销售单
     */
    @Override
    public List<TSalOrder> selectTSalOrderList(TSalOrder tSalOrder)
    {
        return tSalOrderMapper.selectTSalOrderList(tSalOrder);
    }

    /**
     * 新增销售单
     *
     * @param tSalOrder 销售单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertTSalOrder(TSalOrder tSalOrder)
    {
        tSalOrder.setOrderId(IdUtils.simpleUUID());
        tSalOrder.setCreateTime(DateUtils.getNowDate());
        int rows = tSalOrderMapper.insertTSalOrder(tSalOrder);
        insertTSalOrderEntry(tSalOrder);
        return rows;
    }

    /**
     * 修改销售单
     *
     * @param tSalOrder 销售单
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateTSalOrder(TSalOrder tSalOrder)
    {
        tSalOrder.setUpdateTime(DateUtils.getNowDate());
        tSalOrderMapper.deleteTSalOrderEntryByOrderNo(tSalOrder.getId());
        insertTSalOrderEntry(tSalOrder);
        return tSalOrderMapper.updateTSalOrder(tSalOrder);
    }

    /**
     * 批量删除销售单
     *
     * @param ids 需要删除的销售单主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalOrderByIds(Long[] ids)
    {
        tSalOrderMapper.deleteTSalOrderEntryByOrderNos(ids);
        return tSalOrderMapper.deleteTSalOrderByIds(ids);
    }

    /**
     * 删除销售单信息
     *
     * @param id 销售单主键
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteTSalOrderById(Long id)
    {
        tSalOrderMapper.deleteTSalOrderEntryByOrderNo(id);
        return tSalOrderMapper.deleteTSalOrderById(id);
    }

    /**
     * 新增销售单明细信息
     *
     * @param tSalOrder 销售单对象
     */
    public void insertTSalOrderEntry(TSalOrder tSalOrder)
    {
        List<TSalOrderEntry> tSalOrderEntryList = tSalOrder.getTSalOrderEntryList();
        String orderNo = tSalOrder.getOrderNo();
        if (StringUtils.isNotNull(tSalOrderEntryList))
        {
            List<TSalOrderEntry> list = new ArrayList<TSalOrderEntry>();
            for (TSalOrderEntry tSalOrderEntry : tSalOrderEntryList)
            {
                tSalOrderEntry.setOrderNo(orderNo);
                list.add(tSalOrderEntry);
            }
            if (list.size() > 0)
            {
                // 分段插入，数据量太大引起mysql报错
                int steps = list.size() / 20;
                steps ++;
                int start = 0;
                int end = 0;
                for(int step = 0 ; step < steps; step ++){
                    start = step * 20;
                    end = (step + 1) * 20;
                    if(step == ( steps -1 )){
                        end = list.size();
                    }
                    List<TSalOrderEntry> subList = list.subList(start, end);
                    if(!CollectionUtils.isEmpty(subList)){
                        tSalOrderMapper.batchTSalOrderEntry(subList);
                    }
                }
            }
        }
    }


    /**
     *
     * @param tSalOrderList
     */
    @Override
    public void syncOrderByErp(List<TSalOrder> tSalOrderList) {
        TSalOrder tSalOrderCondition = new TSalOrder();
        for(TSalOrder tSalOrder : tSalOrderList){
            tSalOrderCondition.setOrderNo(tSalOrder.getOrderNo());
            List<TSalOrder> orderList = tSalOrderMapper.selectTSalOrderList(tSalOrderCondition);
            if(!CollectionUtils.isEmpty(orderList)){
                TSalOrder orgTSalOrder = orderList.get(0);
                BeanUtils.copyProperties(tSalOrder, orgTSalOrder);
                orgTSalOrder.setTSalOrderEntryList(tSalOrder.getTSalOrderEntryList());
                orgTSalOrder.setUpdateBy("erp");
                orgTSalOrder.setUpdateTime(DateUtils.getNowDate());
                updateTSalOrder(orgTSalOrder);
            } else {
                insertTSalOrder(tSalOrder);
            }
        }
    }
}
